<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="生产计划id,外键" prop="plansId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.plansId"-->
      <!--          placeholder="请输入生产计划id,外键"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="设备表id,外键" prop="shebeiId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.shebeiId"-->
      <!--          placeholder="请输入设备表id,外键"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="产品名称" prop="cpName">
        <el-select v-model="queryParams.cpName" placeholder="请选择产品名称" clearable
                   @keyup.enter.native="handleQuery">
          <el-option v-for="item in plansList" :label="item.cpName" :value="item.cpName"
                     :key="item.ordersId"></el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="加工件名称" prop="gjName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.gjName"-->
      <!--          placeholder="请输入加工件名称"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工时给定" prop="timeSetting">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.timeSetting"-->
      <!--          placeholder="请输入工时给定"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="员工姓名" prop="ygName">
        <el-input
          v-model="queryParams.ygName"
          placeholder="请输入员工姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="工时实际完成时间" prop="actuallyFinish">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.actuallyFinish"-->
      <!--          placeholder="请输入工时实际完成时间"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工作进度" prop="gzSchedule">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.gzSchedule"-->
      <!--          placeholder="请输入工作进度"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
<!--      <el-form-item label="完成数量" prop="quantityCompleted">-->
<!--        <el-input-->
<!--          v-model="queryParams.quantityCompleted"-->
<!--          placeholder="请输入完成数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <!--      <el-form-item label="质量" prop="quality">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.quality"-->
      <!--          placeholder="请输入质量"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工长确认实际工时" prop="querenTime">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.querenTime"-->
      <!--          placeholder="请输入工长确认实际工时"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="填表日期" prop="statementDate">
        <el-date-picker clearable
                        v-model="queryParams.statementDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择填表日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['production:report:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['production:report:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <!--        <el-button-->
        <!--          type="danger"-->
        <!--          plain-->
        <!--          icon="el-icon-delete"-->
        <!--          size="mini"-->
        <!--          :disabled="multiple"-->
        <!--          @click="handleDelete"-->
        <!--          v-hasPermi="['production:report:remove']"-->
        <!--        >删除-->
        <!--        </el-button>-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['production:report:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="日报表id" align="center" prop="reportId"/>-->
      <!--      <el-table-column label="生产计划id,外键" align="center" prop="plansId"/>-->
      <!--      <el-table-column label="设备表id,外键" align="center" prop="shebeiId"/>-->
      <el-table-column label="产品名称" align="center" prop="cpName" width="250" show-overflow-tooltip/>
      <!--      <el-table-column label="加工件名称" align="center" prop="gjName"/>-->
      <!--      <el-table-column label="工作内容和需求" align="center" prop="jobContent"/>-->
      <!--      <el-table-column label="工时给定" align="center" prop="timeSetting"/>-->
      <el-table-column label="员工姓名" align="center" prop="ygName"/>
      <!--      <el-table-column label="工时实际完成时间" align="center" prop="actuallyFinish"/>-->
      <!--      <el-table-column label="工作进度" align="center" prop="gzSchedulePercentage"/>-->
      <el-table-column label="已完成数量" align="center" prop="quantityCompleted"/>
      <!--      <el-table-column label="问题说明" align="center" prop="wtStatement" show-overflow-tooltip/>-->
      <!--      <el-table-column label="质量" align="center" prop="quality"/>-->
      <!--      <el-table-column label="工长确认实际工时" align="center" prop="querenTime"/>-->
      <!--      <el-table-column label="备注" align="center" prop="remark"/>-->
      <el-table-column label="填表日期" align="center" prop="statementDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.statementDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['production:report:edit']"-->
      <!--          >修改-->
      <!--          </el-button>-->
      <!--          &lt;!&ndash;          <el-button&ndash;&gt;-->
      <!--          &lt;!&ndash;            size="mini"&ndash;&gt;-->
      <!--          &lt;!&ndash;            type="text"&ndash;&gt;-->
      <!--          &lt;!&ndash;            icon="el-icon-delete"&ndash;&gt;-->
      <!--          &lt;!&ndash;            @click="handleDelete(scope.row)"&ndash;&gt;-->
      <!--          &lt;!&ndash;            v-hasPermi="['production:report:remove']"&ndash;&gt;-->
      <!--          &lt;!&ndash;          >删除&ndash;&gt;-->
      <!--          &lt;!&ndash;          </el-button>&ndash;&gt;-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作日报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--        <el-form-item label="生产计划id,外键" prop="plansId">-->
        <!--          <el-input v-model="form.plansId" placeholder="请输入生产计划id,外键"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="设备表id,外键" prop="shebeiId">-->
        <!--          <el-input v-model="form.shebeiId" placeholder="请输入设备表id,外键"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="产品名称" prop="cpName">-->
        <!--          <el-input v-model="form.sbName" placeholder="请输入产品名称"/>-->
        <!--        </el-form-item>-->
        <el-row>
          <el-col :span="12">

            <el-form-item label="产品名称" prop="cpName">
              <report-com v-model="form.cpName" @update:parameterList="handleMaterialNumberUpdate"
                          placeholder="请选择产品" clearable/>
            </el-form-item>
            <!--            <el-form-item label="产品名称" prop="cpName">-->
            <!--              <el-select v-model="form.cpName" placeholder="请选择产品名称" clearable-->
            <!--                         @keyup.enter.native="handleQuery"-->
            <!--                         style="width: 100%"-->
            <!--                         @change="getNumber"-->
            <!--              >-->
            <!--                <el-option v-for="item in plansList" :label="item.cpName" :value="item.cpName"-->
            <!--                           :key="item.ordersId"></el-option>-->
            <!--              </el-select>-->
            <!--            </el-form-item>-->
          </el-col>
          <el-col :span="12">
            <el-form-item label="完成数量" prop="quantityCompleted">
              <el-input v-model="form.quantityCompleted" placeholder="请输入已完成数量"
                        oninput="value=value.replace(/[^\d.]/g,'')"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="问题说明" prop="wtStatement">
          <el-input v-model="form.wtStatement" type="textarea" placeholder="请输入内容"/>
        </el-form-item>

        <!--        <el-form-item label="加工件名称" prop="gjName">-->
        <!--          <el-input v-model="form.gjName" placeholder="请输入加工件名称"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="工作内容和需求">-->
        <!--          <el-input type="textarea" v-model="form.jobContent" :min-height="192"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="工时给定" prop="timeSetting">-->
        <!--          <el-input v-model="form.timeSetting" placeholder="请输入工时给定" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="员工姓名" prop="ygName">-->
        <!--          <el-input v-model="form.ygName" placeholder="请输入员工姓名"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="工时实际完成时间" prop="actuallyFinish">-->
        <!--          <el-input v-model="form.actuallyFinish" placeholder="请输入工时实际完成时间"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="工作进度" prop="gzSchedule">-->
        <!--          <el-input v-model="form.gzSchedule" placeholder="请输入工作进度"/>-->
        <!--        </el-form-item>-->


        <!--        <el-form-item label="质量" prop="quality">-->
        <!--          <el-input v-model="form.quality" placeholder="请输入质量"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="工长确认实际工时" prop="querenTime">-->
        <!--          <el-input v-model="form.querenTime" placeholder="请输入工长确认实际工时" />-->
        <!--        </el-form-item>-->

        <!--        <el-form-item label="填表日期" prop="statementDate">-->
        <!--          <el-date-picker clearable-->
        <!--            v-model="form.statementDate"-->
        <!--            type="date"-->
        <!--            value-format="yyyy-MM-dd"-->
        <!--            placeholder="请选择填表日期">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listReport, getReport, delReport, addReport, updateReport} from "@/api/production/report";
import {getDate} from "@/utils/getTime";
import {listPlans} from "@/api/production/plans";
import PlansCom from "@/views/production/plans/plansCom.vue";
import ReportCom from "@/views/production/report/reportCom.vue";

export default {
  name: "Report",
  components: {ReportCom, PlansCom},
  data() {
    return {
      parameterList: {},
      plansList: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作日报表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        plansId: null,
        shebeiId: null,
        sbName: null,
        gjName: null,
        jobContent: null,
        timeSetting: null,
        ygName: null,
        actuallyFinish: null,
        gzSchedule: null,
        quantityCompleted: null,
        wtStatement: null,
        quality: null,
        querenTime: null,
        statementDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cpName: [
          {required: true, trigger: "blur", message: "请选择产品名称"}
        ],
        number: [
          {required: true, trigger: "blur", message: "请输入产品数量"}
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getplansList()
  },
  methods: {
    handleMaterialNumberUpdate(parameterList) {
      this.parameterList = parameterList;
      console.log(this.parameterList)
    },
    /** 查询工作日报列表 */
    getplansList() {
      this.loading = true;
      listPlans().then(response => {
        this.plansList = response.rows;
      });
    },
    getList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reportId: null,
        plansId: null,
        shebeiId: null,
        sbName: null,
        gjName: null,
        jobContent: null,
        timeSetting: null,
        ygName: null,
        actuallyFinish: null,
        gzSchedule: null,
        quantityCompleted: null,
        wtStatement: null,
        quality: null,
        querenTime: null,
        remark: null,
        statementDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.reportId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工作日报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reportId = row.reportId || this.ids
      getReport(reportId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工作日报";
      });
    },
    getNumber() {
      this.plansList.forEach((item) => {
        if (this.form.cpName == item.cpName) {
          this.form.plansId = item.plansId;
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.plansId = this.parameterList.plansId
          if (this.form.reportId != null) {
            console.log(this.parameterList.scSum, 'jihua')
            console.log(this.form.quantityCompleted, 'wancheng')
            if (this.parameterList.scSum < this.form.quantityCompleted) {
              this.$modal.msgError("不可超出计划数量" + this.parameterList.scSum);
            } else {
              updateReport(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
            if (this.parameterList.scSum < this.form.quantityCompleted) {
              this.$modal.msgError("不可超出计划数量" + this.parameterList.scSum);
            } else {
              this.form.plansId = this.parameterList.plansId
              this.form.ygName = this.$store.state.user.name
              this.form.statementDate = getDate()
              addReport(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }

          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reportIds = row.reportId || this.ids;
      this.$modal.confirm('是否确认删除工作日报编号为"' + reportIds + '"的数据项？').then(function () {
        return delReport(reportIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('production/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
